package com.macro.mall.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson.JSON;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.BasicKeyRegionVo;
import com.macro.mall.mapper.BasicKeyRegionMapper;
import com.macro.mall.model.BasicKeyRegion;
import com.macro.mall.query.RegionQuery;
import com.macro.mall.service.SysAreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysAreaServiceImpl implements SysAreaService {

    @Autowired
    private BasicKeyRegionMapper basicKeyRegionMapper;

    @Override
    public CommonResult list(RegionQuery query) {
        List<BasicKeyRegion> basicKeyRegions = basicKeyRegionMapper.findAll(query);
        if (basicKeyRegions.isEmpty()) {
            return CommonResult.success(null);
        }

        List<String> list = new ArrayList<>();
        list.add("上海");
        list.add("北京");
        list.add("广州");
        //转树
        List<Tree<String>> build = TreeUtil.build(basicKeyRegions, "0", (treeNode, tree) -> {
            tree.setId(String.valueOf(treeNode.getId()));
            tree.setName(treeNode.getName());
            tree.setParentId(String.valueOf(treeNode.getPid()));
            tree.putExtra("status", treeNode.getStatus());
            tree.putExtra("createdDt", treeNode.getCreatedDt());
            tree.putExtra("alias", JSON.parseArray(treeNode.getAlias(), String.class));
            tree.putExtra("remark", treeNode.getRemark());
        });
        return CommonResult.success(build);
    }

    @Override
    public CommonResult modify(BasicKeyRegionVo regionVo) {
        BasicKeyRegion basicKeyRegion = new BasicKeyRegion();
        BeanUtils.copyProperties(regionVo, basicKeyRegion);
        basicKeyRegion.setUpdatedDt(new Date());
        if (regionVo.getAliasList() != null && !regionVo.getAliasList().equals("")) {
            basicKeyRegion.setAlias(JSON.toJSONString(regionVo.getAliasList()));
        }
        int row = basicKeyRegionMapper.updateByPrimaryKeySelective(basicKeyRegion);
        return result(row, "修改失败");
    }


    @Override
    public CommonResult add(BasicKeyRegionVo region) {
        BasicKeyRegion basicKeyRegion = new BasicKeyRegion();
        BeanUtils.copyProperties(region, basicKeyRegion);
        List<String> aliasList = region.getAliasList();

        basicKeyRegion.setStatus("1");
        basicKeyRegion.setDelStatus("0");
        basicKeyRegion.setCreatedDt(new Date());
        basicKeyRegion.setUpdatedDt(new Date());
        basicKeyRegion.setAlias(JSON.toJSONString(aliasList));
        int row = basicKeyRegionMapper.insertSelective(basicKeyRegion);
        return result(row, "添加失败");
    }


    /***
     * 删除 地区
     * @param id
     * @return
     */
    @Override
    public CommonResult delete(Long id) {
        int row = basicKeyRegionMapper.deleteByPrimaryKey(id);
        return result(row, "删除失败");
    }

    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private CommonResult result(int row, String msg) {
        if (row > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed(msg);
    }
}
